<template>
  <basic-container :btnFlag=true>
    <div style="border: 1px solid #d2d2d2;">
      <card-header :header-data="headerData"
                   @buttonClick="handleAdd()"></card-header>
      <div style="margin: 20px;">
        <el-row>
          <el-col :span="18"
                  style="margin-bottom: -43px;">
            <el-form :label-position="labelPosition"
                     label-width="80px"
                     :model="projectForm"
                     style="padding-right: 46%;padding-left: 10px;"
                     :rules="rules1"
                     ref="projectForm">

              <el-form-item class="form-item"
                            label="清单标题"
                            prop="title">
                <el-input v-model="projectForm.title"></el-input>
              </el-form-item>

              <el-form-item class="form-item"
                            label="清单说明"
                            prop="description">
                <el-input type="textarea"
                          :rows="3"
                          v-model="projectForm.description">
                </el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6"
                  style="margin-bottom: -7px;">
            <div>
              <el-button @click.stop="adjustment()"
                         type="primary"
                         style="float:right;margin-left: 31px;margin-top: 113px;">选择规划</el-button>
            </div>
          </el-col>
        </el-row>
      </div>

      <el-dialog title="请选择规划"
                 :visible.sync="dialogFormVisible"
                 width="85%">
        <avue-crud :data="datadialog"
                   :option="optiondialog"
                   :page="pagedialog"
                   :table-loading="Loadingdialog"
                   @on-load="getListdialog"
                   @refresh-change="refreshdialog"
                   @selection-change="selectionChange"
                   style="margin-top: -75px;">
          <template slot-scope="scope"
                    slot="menu">
            <div class="menu-title"
                 @click.stop="">
              <i class="iconfont iconview"
                 style="color: #409EFF;"></i>
              <p class="menu-title-text">详情</p>
            </div>
          </template>
        </avue-crud>

        <div slot="footer"
             class="dialog-footer">
          <el-button @click="cancelDept()">取 消</el-button>
          <el-button type="primary"
                     @click="confirmDept()">确 定</el-button>
        </div>
      </el-dialog>

      <!--:page="page"-->
      <avue-crud :data="tableData3"
                 :option="option"
                 :table-loading="tableLoading"
                 @on-load="getApplyList"
                 @refresh-change="refreshChange"
                 class="crudcs">
        <template slot-scope="scope"
                  slot="menu">
          <div class="menu-title"
               @click.stop="detailApplication(scope.row,scope.index)">
            <i class="iconfont iconview"
               style="color: #409EFF;"></i>
            <p class="menu-title-text">详情</p>
          </div>
        </template>
      </avue-crud>
      <detail-dialog :reg-id="currentRegisterId"
                     v-if="currentRegisterId"
                     :detail-visiblesubmit="registerDetailVisible"
                     @detailConfirm="detailConfirm">
      </detail-dialog>
      <div style="text-align: center; margin-top: 40px;margin-bottom: 40px;">
        <el-button type="warning"
                   @click="submitList()">修改并提交</el-button>
        <el-button @click="goback()">取消</el-button>
      </div>
    </div>
  </basic-container>
</template>
<script>
import { protocolAdd } from "@/api/planning/detailList";
import { projectRegister } from "@/api/planning/project";
import { getbasisinfo } from "@/api/planning/detailList";
import { protocolEditsubmit } from "@/api/planning/detailList";
import { projectListAll } from "@/api/planning/project";
import { PROJECT_DIC } from '@/const/dict'
import detailDialog from "@/components/bostech/detailDialog";

export default {
  name: "protocolEditsubmit",
  components:{
    detailDialog
  },
  data () {
    return {
      tableLoading: false,
      Loadingdialog: false,//选择规划类型表单加载标志
      dialogFormVisible: false,//选择规划类型标志
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      pagedialog: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      projectForm: {
        title: '',
        description: '',
        registerinfoIds: [],
      },
      editForm: {
        id: '',
        projectform: {},
      },
      headerData: {
        title: '清单拟定编辑',
        buttonName: ''
      },
      labelPosition: 'right',

      tableData3: [],
      datadialog: [],
      tableDatatemp: [],
      rules1: {
        title: [
          { required: true, message: '请输入清单标题', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入清单说明', trigger: 'blur' }
        ],
      },
      option: {
        page: true,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        indexLabel: "序号",
        index: true,
        indexWidth:70,
        cellBtn: false,
        delBtn: false,
        columnBtn: false,
        refreshBtn: false,
        filterBtn: false,
        searchBtn: false,
        keyId: "id",
        menuWidth: 100,
        column: [
          {
            label: "规划名称",
            width: 440,
            prop: "title",
            cell: true,
          },
          // {
          //   label: "规划期限",
          //   prop: "start_end",
          //   cell: true,
          //   formatter: function (row, value, label, column) {
          //     return row.startDate + '-' + row.endDate;
          //   }
          // },
          {
            label: "申报(牵头)单位",
            width: 240,
            prop: "org_code",
            cell: true,
          },
          {
            label: "规划类型",
            prop: "category",
            type: "select",
            dicData: PROJECT_DIC.CATEGORY,
            cell: true
          }
        ]
      },

      optiondialog: {
        page: true,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        indexLabel: "序号",
        index: true,
        indexWidth:70,
        cellBtn: false,
        delBtn: false,
        columnBtn: false,
        refreshBtn: false,
        filterBtn: false,
        searchBtn: false,
        keyId: "id",
        selection: true,
        column: [
          {
            label: "规划名称",
            width: 240,
            prop: "title",
            cell: true,
          },
          // {
          //   label: "规划期限",
          //   prop: "start_end",
          //   cell: true,
          //   formatter: function (row, value, label, column) {
          //     return row.startDate + '-' + row.endDate
          //   }
          // },
          {
            label: "申报(牵头)单位",
            width: 240,
            prop: "org_code",
            cell: true,
          },
          {
            label: "规划类型",
            prop: "category",
            type: "select",
            dicData: PROJECT_DIC.CATEGORY,
            cell: true
          }
        ]
      },

      registerDetailVisible: false,
      currentRegisterId: null,

    }
  },
  mounted () {
    this.getPreList();
  },
  methods: {
    submitList () {
      this.$confirm("确定修改并提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        for (var item in this.tableData3) {
          this.projectForm.registerinfoIds.push(this.tableData3[item].registerinfoId);
        }
        this.projectForm.registerinfoIds = this.qvchong(this.projectForm.registerinfoIds);
        this.editForm.projectform = this.projectForm;
        this.$refs.projectForm.validate(valid => {
          if (valid) {
            protocolEditsubmit(this.editForm).then(response => {
              if (response.data.code == 0) {
                this.$notify({
                  title: '成功',
                  message: '更新成功',
                  type: 'success',
                  duration: 2000
                });
                this.$router.go(-1);
              }
            })
          }
        });
      })
    },

    getApplyList (page, params) {

    },
    getListdialog (page, params) {
      this.Loadingdialog = true;
      projectListAll(Object.assign({
        current: page.currentPage,
        size: page.pageSize,
        taskKey: 'list_report',
        inList: false,
        listId: this.$route.query.listId,
      })).then(response => {
        this.datadialog = response.data.data.records;
        this.pagedialog.total = response.data.data.total;
        this.Loadingdialog = false
      })
    },

    getPreList () {
      //得到清单详情
      getbasisinfo(this.$route.query.listId).then(response => {
        this.editForm.id = this.$route.query.listId;
        this.projectForm.title = response.data.data.title;
        this.projectForm.description = response.data.data.description;
        this.tableData3 = response.data.data.planningRegisterinfoList;
      })
    },


    goback () {
      this.$router.go(-1);
    },
    adjustment () {
      this.dialogFormVisible = true;
    },

    refreshChange () {
      this.getProtocolList(this.page)
    },
    refreshdialog () {
      this.getProtocolList(this.page)
    },
    selectionChange (list) {
      this.tableDatatemp = list;
    },
    cancelDept () {
      this.dialogFormVisible = false;
    },
    confirmDept () {
      this.tableData3 = this.tableDatatemp;
      this.dialogFormVisible = false;
    },
    qvchong (sort) {
      var result = [];
      var temp = true;
      var sorttemp = {};
      for (var item in sort) {
        temp = true;
        for (var itemresult in result) {
          if (result[itemresult] == sort[item]) {
            temp = false; //说明数组中有此对象
          }
        }
        if (temp == true) {
          result.push(sort[item]);
        }
      }
      return result;
    },
    // 立项详情
    detailApplication (row, index) {
      this.currentRegisterId = row.registerinfoId;
      this.registerDetailVisible = true
    },
    // 详情弹框确认
    detailConfirm () {
      this.registerDetailVisible = false
    }

  }
}
</script>



